#include "PluginManager.h"

#include <QDir>
#include <QPluginLoader>
#include <QApplication>

#include "widget/PluginManagerDialog.h"
#include "widget/MainWindow.h"

PluginManager PluginManager::sInst;

class PluginManagerPrivate{
public:
    QHash<QString,BasePlugin*> mPluginMap;
    friend class PluginManager;
};

void PluginManager::PreLoadPlugin(QString pluginpath)
{
    _p->mPluginMap.clear();
    //插件预加载
    QDir pluginsDir(pluginpath);
    foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
        QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
        QObject *obj = pluginLoader.instance();
        if (obj) {
            auto pPlugin = qobject_cast<BasePlugin *>(obj);
            pPlugin->PreLoad();
            _p->mPluginMap.insert(pPlugin->GetName(), pPlugin);
        }
    }

}

void PluginManager::LoadPlugin(int argc, char **argv)
{
    auto itSysPlugin = _p->mPluginMap.begin();
    while(itSysPlugin != _p->mPluginMap.end()){
        if(itSysPlugin.value()->GetType().contains(PluginType::System)){
            itSysPlugin.value()->OnLoad(argc,argv);
        }
        itSysPlugin++;
    }

    //用户插件加载
    auto itUserPlugin = _p->mPluginMap.begin();
    while(itUserPlugin != _p->mPluginMap.end()){
        if(itUserPlugin.value()->GetType().contains(PluginType::User)){
            itUserPlugin.value()->OnLoad(argc,argv);
        }
        itUserPlugin++;
    }
}

void PluginManager::AfterLoadPlugin()
{
    //插件加载完毕
    auto itPlugin = _p->mPluginMap.begin();
    while(itPlugin != _p->mPluginMap.end()){
        itPlugin.value()->AfterLoad();

        if(itPlugin.value()->HasType(TopWidget)){
            sMainWindow->SetTopWidget(itPlugin.value()->GetWidget());
        }

        if(itPlugin.value()->HasType(LeftWidget)){
            sMainWindow->SetLeftWidget(itPlugin.value()->GetWidget());
        }

        if(itPlugin.value()->HasType(ButtomWidget)){
            sMainWindow->SetButtomWidget(itPlugin.value()->GetWidget());
        }

        if(itPlugin.value()->HasType(RightWidget)){
            sMainWindow->SetRightWidget(itPlugin.value()->GetWidget());
        }

        if(itPlugin.value()->HasType(CenterWidget)){
            sMainWindow->SetCenterWidget(itPlugin.value()->GetWidget());
        }

        sMainWindow->showFullScreen();
        QString stylefile = QApplication::applicationDirPath()+"/qss/sytle1.qss";
        QFile file(stylefile);
        if(file.open(QIODevice::ReadOnly)){
            sMainWindow->setStyleSheet(file.readAll());
        }


        itPlugin++;
    }
}

const QHash<QString, BasePlugin *> &PluginManager::GetAllPlugins()
{
    return _p->mPluginMap;
}



bool PluginManager::ChoosePlugin()
{
    QHash<QString,BasePlugin*> choosedPlugins;
    PluginManagerDialog dialog;
    auto choose = dialog.ChoosePlugin(_p->mPluginMap,choosedPlugins);
    if(choose){
        _p->mPluginMap = choosedPlugins;
    }else{

    }
    return choose;

}

BasePlugin *PluginManager::GetPlugin(QString name)
{
    if(_p->mPluginMap.contains(name)){
        return _p->mPluginMap[name];
    }else{
        return nullptr;
    }

}

PluginManager::PluginManager():QObject(),_p(new PluginManagerPrivate)
{

}

PluginManager::~PluginManager()
{
    if(_p){
        delete _p;
        _p = nullptr;
    }
}
